var base_url 	=	"http://192.168.0.100:8080/mockapi/security/login.action?username=test&password=test";
//ajax封装
var ajax 	=	function (type,url,data, flag) {
	url 		=	base_url;
	var oAjax 	=	new XMLHttpRequest();
	oAjax.open(type,url,true);
	oAjax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset-UTF-8');
	oAjax.send(data);
	oAjax.onreadystatechange=function(){
		if (oAjax.readyState == 4 && oAjax.status==200) {
			var data_result 	=	oAjax.responseText;
			ajax_callback(data_result,flag);
		}
	}
}
//ajax回调
var ajax_callback 	=	function(data, flag){
	var oJson 		=	JSON.parse(data);
	var errorcode	=	oJson.errorcode;
	var msg 		=	oJson.msg;
	switch(errorcode){
		case 200:
			alert(msg);
			break;
		default:
			alert(msg);
			break;
	}

	if (errorcode == 200) {
		switch(flag){
			case "login":
			login_callback(oJson.data);
			break;
		}	
	}
}
//login回调--把数据存到cookie中
var login_callback 	=	function(data){
    setCookie('nick_name', data.nick_name, 1);
    setCookie('token', data.token, 1);
    setCookie('face', data.face, 1);
    setCookie('user_id', data.user_id, 1);

    window.location.href	=	"./main/index.html";
}
//设置cookie
var setCookie	=	function(name,value,day){
    oDate	=	new Date();
    oDate.setDate(oDate.getDate()+day);
    document.cookie 	=	name+"="+value+";expires="+oDate;
}
//获取cookie
var getCookie	=	function (name) {
    var cookie_str 	=	document.cookie;
    var cookie_arr	=	cookie_str.split('; ');
    for (var i = 0; i < cookie_arr.length; i++) {
        cookie_one_arr	=	cookie_arr[i].split('=');
        if (cookie_one_arr[0] == name) {
            return	cookie_one_arr[1];
        }
    }
    return null;
}
//删除cookie
var removeCookie	=	function(name){
    setCookie(name,'0',-1);
}
// 验证登录
var check_login = function(){
    var token = getCookie('token');
    if(token==""||token==null){
        window.location.href = "./login.html";
    }
}
